<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of category
 * 
 * @author Subha Sundar Das (subhasundar.das@gmail.com)
 */
class Location extends CI_Controller {

    //put your code here


    function location() {
        parent :: __construct();
        $this->load->helper('url');
        $this->load->model('location_model');
        $this->load->model('listing_model');
        $this->load->model('counter_model');
        $this->load->library('form_validation');
        $this->load->library('pagination');
        $this->commoncall->check_admin_login();
    }

    function country() {
        $segment_array = $this->uri->segment_array();
        $segment_count = $this->uri->total_segments();
        $config['base_url'] = base_url() . 'admin/location/country/';
        $config['total_rows'] = $this->db->count_all('country');
        $config['per_page'] = '10';
        $config['uri_segment'] = '4';
        $config['full_tag_open'] = '';
        $config['full_tag_close'] = '';
        $this->pagination->initialize($config);
        $data['country'] = $this->location_model->country_list($config['per_page'], $this->uri->segment(4));
        $this->load->view('admin/admin_country', $data);
    }

    function state() {
        $country_id = $this->uri->segment(4);
        $per_page = 10;
        $total = $this->counter_model->state_count($country_id);
        $config['base_url'] = base_url() . 'admin/location/state/' . $country_id;
        $config['total_rows'] = $total;
        $config['per_page'] = $per_page;
        $config['uri_segment'] = '5';
        $this->pagination->initialize($config);
        $data['state'] = $this->location_model->state_list($per_page, $this->uri->segment(5), $country_id);
        $data['country_id'] = $country_id;
        $this->load->view('admin/admin_state', $data);
    }

    function city() {

        $country_id = $this->uri->segment(5);
        $state_id = $this->uri->segment(4);
        $per_page = 10;
        $total = $this->counter_model->city_count($state_id);
        $config['base_url'] = base_url() . 'admin/location/city/' . $state_id . '/' . $country_id;
        $config['total_rows'] = $total;
        $config['per_page'] = $per_page;
        $config['uri_segment'] = '6';
        $data['city'] = $this->location_model->city_list($state_id, $per_page, $this->uri->segment(6));
        $this->pagination->initialize($config);
        $data['state_id'] = $state_id;
        $data['country_id'] = $country_id;
        $this->load->view('admin/admin_city', $data);
    }

    function country_insert() {
        if ($this->input->post('insert')) {
            $country_detail = $_POST['country'];
            $this->form_validation->set_rules('country', 'country', 'required|xss_clean|is_unique[country.country]');
            if ($this->form_validation->run()) {
                if ($country_detail != 'Enter Country')
                    $this->location_model->country_insert($country_detail);
                else
                    $this->session->set_flashdata('message', 'Type a Country Name');
                redirect('admin/location/country');
            } else {
                $this->session->set_flashdata('message', 'Country already exists');
                redirect('admin/location/country');
            }
        }
    }

    function state_insert() {
        if ($this->input->post('insert')) {
            $state_detail = $_POST['state'];
            $country_id = $_POST['country_id'];
            $this->form_validation->set_rules('state', 'state', 'required|xss_clean|is_unique[state.state]');
            if ($this->form_validation->run()) {
                if ($state_detail != 'Enter States')
                    $this->location_model->state_insert($state_detail, $country_id);
                else
                    $this->session->set_flashdata('message', 'Type a state name');
            } else {
                $this->session->set_flashdata('message', 'State already exists');
            }
        }
        redirect('admin/location/state/' . $country_id);
    }

    function city_insert() {

        if ($this->input->post('insert')) {
            $city_detail = $_POST['city'];
            $state_id = $_POST['state_id'];
            $country_id = $_POST['country_id'];
            $this->form_validation->set_rules('city', 'city', 'required|xss_clean|is_unique[city.city]');
            if ($this->form_validation->run()) {
                if ($city_detail != 'Enter City')
                    $this->location_model->city_insert($city_detail,$state_id, $country_id);
                else
                    $this->session->set_flashdata('message', 'Type a City Name');
            } else {
                $this->session->set_flashdata('message', 'City already exists');
            }
        }
        redirect('admin/location/city/' . $state_id . '/' . $country_id);
    }

    function country_update() {
        $country_id = $_POST['country_id'];
        $country_detail = $_POST['country_detail'];
        $this->form_validation->set_rules('country_id', 'country_id', 'required|xss_clean');
        $this->form_validation->set_rules('country_detail', 'country_detail', 'required|xss_clean');
        if ($this->form_validation->run()) {
                $this->location_model->country_update($country_id, $country_detail);
        }
        redirect('admin/location/country');
    }

    function state_update() {
        $state_id = $_POST['state_id'];
        $state_detail = $_POST['state_detail'];
        $country_id = $_POST['country_id'];
        $this->form_validation->set_rules('state_id', 'state_id', 'required|xss_clean');
        $this->form_validation->set_rules('state_detail', 'state_detail', 'required|xss_clean');
        if ($this->form_validation->run()) {
                      $this->location_model->state_update($state_id, $state_detail);
        }
        redirect('admin/location/state/' . $country_id);
    }

    function city_update() {
        $city_id = $_POST['city_id'];
        $state_id = $_POST['state_id'];
        $country_id = $_POST['country_id'];
        if (isset($_POST['display_top']))
            $dispaly_top = 'yes';
        else
            $dispaly_top = 'no';
        if (isset($_POST['display_bottom']))
            $display_bottom = 'yes';
        else
            $display_bottom = 'no';
        $city_detail = $_POST['city_detail'];
        $this->form_validation->set_rules('city_id', 'city_id', 'required|xss_clean');
        $this->form_validation->set_rules('city_detail', 'city_detail', 'required|xss_clean');

        if ($this->form_validation->run()) {

            $this->location_model->city_update($city_id, $city_detail, $dispaly_top, $display_bottom);
        }
        redirect('secure/location/city/' . $state_id . '/' . $country_id);
    }

    function country_delete() {
        $country_id = $this->uri->segment(4);
        $this->location_model->country_delete($country_id);
        redirect('admin/location/country');
    }

    function state_delete() {
        $state_id = $this->uri->segment(5);
        $country_id = $this->uri->segment(4);
        $this->location_model->state_delete($state_id);
        redirect('admin/location/state/' . $country_id);
    }

    function city_delete() {
        $city_id = $this->uri->segment(6);
        $state_id = $this->uri->segment(5);
        $country_id = $this->uri->segment(4);
        $this->location_model->city_delete($city_id);
        redirect('admin/location/city/' . $state_id . '/' . $country_id);
    }

}

?>
